<template>
    <a-drawer :title="title" :width="400" @close="handleCancel(false)" :visible="visible" :maskClosable="false"
              :bodyStyle="{ paddingBottom: '60px' }">
        <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">

            <a-form-model-item label="设备代码" prop="code" ref="code" help="写设备的唯一代码 一般为设备的MAC地址或出厂序列号">
                <a-input placeholder="请填写设备代码" v-model="form.code"
                         :disabled="isUpdate"
                />
            </a-form-model-item>



            <a-form-model-item label="安装状态" prop="deployStatus" ref="deployStatus">
                <a-radio-group v-model="form.deployStatus" button-style="solid" @change="onChangRadio">
                    <a-radio-button v-for="(item,index) in DeployStatusOPT " :key="index" :value="item.value">
                        {{item.label}}
                    </a-radio-button>
                </a-radio-group>
            </a-form-model-item>



            <a-form-model-item label="所属场地" prop="placeCode" ref="placeCode" v-if="form.deployStatus==='T'">
                <a-select placeholder="请选择设备所属场地" allowClear
                          showSearch :filter-option="$exMod.cxOption"
                          v-model="form.placeCode">
                    <a-select-option v-for="(item, index) in PlaceOPT" :key="index" :value="item.value">
                        {{item.label }}
                    </a-select-option>
                </a-select>
            </a-form-model-item>


        </a-form-model>
        <!-- Modal 页脚 -->
        <footer class="drawer-footer">
            <a-button type="primary" :loading="loading" @click="onSubmit">保存</a-button>
        </footer>
    </a-drawer>
</template>
<script>
    import {deviceAdd, deviceUpdate} from '../public/http.js';
    import {PlaceDict} from '../public/internal'
    import {dictLoad} from '../../../util/internal.api';

    export default {
        props: ['editRecord'],
        data() {
            return {
                isUpdate: false, //是否是更新模式
                loading: false, //按钮Loading
                title: '新增设备',
                visible: true, //modal 显示状态
                labelCol: {
                    xs: {
                        span: 24
                    },
                    sm: {
                        span: 6
                    }
                },
                wrapperCol: {
                    xs: {
                        span: 24
                    },
                    sm: {
                        span: 16
                    }
                },
                form: {
                    code: null,
                    name: null,
                    placeCode: undefined,
                    deployStatus: undefined,

                },
                rules: {
                    code: [
                        {
                            required: true,
                            message: '请设置设备代码',
                            trigger: 'blur'
                        },
                        {
                            max: 20,
                            message: '设备代码请设置在20个字符以内',
                            trigger: 'blur'
                        }

                    ],
                    placeCode: {
                        required: true,
                        message: '请选择设备投放安装的场地',
                        trigger: 'blur'
                    },

                    deployStatus:{
                        required: true,
                        message: '请设置设备的安装状态',

                        trigger: 'blur'
                    }

                },
                PlaceOPT: [],
                DeployStatusOPT: [],

            };
        },
        methods: {

            onChangRadio(e) {
                this.form.deployStatus = e.target.value;
                if(e.target.value !== 'T'){
                    this.form.placeCode=null;
                }
            },

            //表单提交事件
            onSubmit() {
                this.$refs.ruleForm.validate(valid => {
                    //验证成功后进行数据提交
                    if (valid) {
                        this.loading = true;
                        if (this.isUpdate) {
                            deviceUpdate(this.form.id, this.form).then(res => {
                                this.$exMod.ajaxMessage(res);
                                if (res.code === 0) {
                                    this.handleCancel(true);
                                }
                            });
                        } else {
                            deviceAdd(this.form).then(res => {
                                this.$exMod.ajaxMessage(res);
                                if (res.code === 0) {
                                    this.handleCancel(true);
                                }
                            });
                        }
                        this.loading = false;
                    }
                });
            },
            //关闭弹窗 刷新
            handleCancel(render = false) {
                this.visible = false;
                this.$emit('closeFun', {render: render});
            },
            //表单初始化
            fromVal() {
                let obj = {...this.editRecord};
                if (obj.id) {
                    this.form = obj;
                    this.isUpdate = true;
                    this.title = '编辑设备';
                }
            },
            //数据初始化
            async init() {
                await PlaceDict().then(res => this.PlaceOPT = res);
                await dictLoad("DeployStatus").then(res => this.DeployStatusOPT = res);
                this.fromVal();
            }
        },
        created() {
            this.init();
        }
    };
</script>
